package cn.kgc.springcloud.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.kgc.springcloud.entity.CollectionDTO;
import cn.kgc.springcloud.entity.Product;
import cn.kgc.springcloud.feignclient.ProductClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/9
 */
@RestController
@RequestMapping("order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private ProductClient productClient;

    @RequestMapping("order1")
    public String test01(){

        String s = productClient.test01();

        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }


    @RequestMapping("order2")
    public String test02(Integer id){
        logger.info("前端发送的id:{}",id);
        String s = productClient.test02(id);
        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }

    @RequestMapping("order3/{id}")
    public String test03(@PathVariable("id") Integer id){
        logger.info("前端发送的id:{}",id);
        String s = productClient.test03(id);
        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }


    @RequestMapping("order4")
    public String test04(Product product){
        logger.info("前端发送的product:{}",product);
        String s = productClient.test04(product);
        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }

    @RequestMapping("order5")
    public String test04(String[] ids){
        for (int i = 0; i < ids.length; i++) {
            logger.info("ids={}",ids[i]);
        }
        String s = productClient.test05(ids);
        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }

    @RequestMapping("order6")
    public String test05(CollectionDTO collectionDTO){
        ArrayList<Integer> ids = collectionDTO.getIds();
        for (int i = 0; i < ids.size(); i++) {
            logger.info("ids={}",ids.get(i));
        }
        String s = productClient.test06(collectionDTO);
        logger.info("商品模块被调用，返回值是：{}",s);
        return  "订单服务被调用";
    }

    @RequestMapping("order7")
    public Product  test06(Integer id){
        Product product = productClient.test07(id);
        logger.info("{}",product);
        return  product;
    }

    @RequestMapping("order8")
    public ArrayList<Product> test07(Integer id){
        ArrayList<Product> products = productClient.test08();
        for (Product product : products) {
            logger.info("{}",product);
        }
        return  products;
    }

    @RequestMapping("order9")
    public String test09(Integer id){
        String info = productClient.test09();  // json类型的字符串

        JSONObject jsonObject = JSONUtil.parseObj(info);
        Object data = jsonObject.get("data");

        List<Product> products = JSONUtil.toList(data.toString(), Product.class);

        for (Product product : products) {
            System.out.println("product = " + product);
        }

        return  info;
    }

}
